# -*- coding: utf-8 -*-
# @Time    :2022/3/1 20:16
# @AUTHOR  :MORAN
# @Email   :moran@maqu.com
# @File    :permissions.py
# @Software:PyCharm
from rest_framework.response import Response


def create_auto_user(func):
    def f(self, request, *args, **kwargs):
        request.POST._mutable = True  # 讲post请求携带的参数设置为可修改
        request.data['user'] = str(request.user.id)

        return func(self, request, *args, **kwargs)
    return f


def update_auto_user(model):
    def wrapper(func):
        def f(self, request, *args, **kwargs):
            if not model.objects.filter(id=kwargs['pk'], user=request.user):
                return Response({'detail': '您没有修改该文章的权限'})
            request.POST._mutable = True  # 讲post请求携带的参数设置为可修改
            request.data['user'] = str(request.user.id)
            return func(self, request, *args, **kwargs)
        return f
    return wrapper


def destroy_auto_user(model):
    def wrapper(func):
        def f(self, request, *args, **kwargs):
            if not model.objects.filter(id=kwargs['pk'], user=request.user):
                return Response({'detail': '您没有删除该文章的权限'})
            return func(self, request, *args, **kwargs)
        return f
    return wrapper

